package org.jeecg.modules.assets.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.assets.dto.AssetsListDTO;
import org.jeecg.modules.assets.dto.OptionAssetsItemDTO;
import org.jeecg.modules.assets.dto.OptionPreliminaryAssetsItemDTO;
import org.jeecg.modules.assets.dto.StartAssetsDTO;
import org.jeecg.modules.assets.entity.AssetsItem;
import org.jeecg.modules.assets.entity.AssetsPreliminaryResults;
import org.jeecg.modules.assets.service.IAssetsItemService;
import org.jeecg.modules.assets.service.IAssetsPreliminaryResultsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 初盘结果管理
 * @Author: jeecg-boot
 * @Date: 2023-12-12
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "移动端初盘结果管理")
@RestController
@RequestMapping("/app/assets/assetsPreliminaryResults")
public class AppAssetsPreliminaryResultsController extends JeecgController<AssetsPreliminaryResults, IAssetsPreliminaryResultsService> {
    @Autowired
    private IAssetsPreliminaryResultsService assetsPreliminaryResultsService;
    @Autowired
    private IAssetsItemService assetsItemService;

    @Autowired
    private ISysBaseAPI iSysBaseAPI;

    /**
     * 分页列表查询
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @param
     * @return
     */
    @AutoLog(value = "初盘结果管理-获取用户初盘信息")
    @ApiOperation(value = "初盘结果管理-获取用户初盘信息", notes = "初盘结果管理-获取用户初盘信息")
    @GetMapping(value = "/getUsetAssetsInfo")
    public Result<AssetsListDTO> getUsetAssetsInfo(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "billNo") String billNo,
            @RequestParam(name = "queryType", required = false, defaultValue = "1") String queryType,
            @RequestParam(name = "assetName", required = false) String assetName,
            @RequestParam(name = "asset", required = false) String asset,
            @RequestParam(name = "inventoryStatus", required = false) String inventoryStatus,
            @RequestParam(name = "preliminaryStatus", required = false) Integer preliminaryStatus,
              @RequestParam(name = "company", required = false) String company
    ) {
        LambdaQueryWrapper<AssetsPreliminaryResults> lambda = new QueryWrapper<AssetsPreliminaryResults>().lambda();
        lambda.eq(AssetsPreliminaryResults::getBillNo, billNo);
        AssetsPreliminaryResults assetsPreliminaryResults = assetsPreliminaryResultsService.getOne(lambda);
        Page page = new Page<>(pageNo, pageSize);
        AssetsListDTO assetsListDTO = assetsPreliminaryResultsService.getUsetAssetsInfo(assetsPreliminaryResults, page, queryType, assetName, inventoryStatus, preliminaryStatus,company,asset);
        return Result.OK(assetsListDTO);
    }

    @AutoLog(value = "初盘结果管理-分页列表查询")
    @ApiOperation(value = "初盘结果管理-分页列表查询", notes = "初盘结果管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(AssetsPreliminaryResults assetsPreliminaryResults,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<AssetsPreliminaryResults> queryWrapper = QueryGenerator.initQueryWrapper(assetsPreliminaryResults, req.getParameterMap());
        Page<AssetsPreliminaryResults> page = new Page<AssetsPreliminaryResults>(pageNo, pageSize);
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //只获取该用户当前部门的列表
        queryWrapper.eq("dept_id", loginUser.getDepartIds());
        //回去用户关联的资产的部门，并查询初盘信息
        //todo
        //安创建时间倒序
        queryWrapper.orderByDesc("start_time");
        IPage<AssetsPreliminaryResults> pageList = assetsPreliminaryResultsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @AutoLog(value = "初盘结果管理-获取单据状态")
    @ApiOperation(value = "初盘结果管理-获取单据状态", notes = "初盘结果管理-获取单据状态")
    @GetMapping(value = "/queryStatusByBillNo")
    public Result<?> queryStatusByBillNo(
            @RequestParam(name = "billNo", required = false) String billNo,
            @RequestParam(name = "queryType", defaultValue = "1") String queryType) {

        String status = assetsPreliminaryResultsService.queryStatusByBillNo(billNo, queryType);
        return Result.OK(status);
    }

    /**
     * 添加
     *
     * @param assetsPreliminaryResults
     * @return
     */
    @AutoLog(value = "初盘结果管理-添加")
    @ApiOperation(value = "初盘结果管理-添加", notes = "初盘结果管理-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody AssetsPreliminaryResults assetsPreliminaryResults) {
        assetsPreliminaryResultsService.save(assetsPreliminaryResults);
        return Result.OK("添加成功！");
    }


    @AutoLog(value = "初盘结果管理-操作初盘明细")
    @ApiOperation(value = "初盘结果管理-操作初盘明细", notes = "初盘结果管理-操作初盘明细")
    @PostMapping(value = "/optionAssetsItem")
    public Result optionAssetsItem(@RequestBody OptionPreliminaryAssetsItemDTO optionPreliminaryAssetsItem) {
        try {
            assetsPreliminaryResultsService.optionAssetsItem(optionPreliminaryAssetsItem);
        } catch (Exception e) {
            log.error("操作初盘明细失败", e);
            return Result.error("操作初盘明细失败:" + e.getMessage());
        }
        return Result.OK();
    }

    /**
     * 添加
     *
     * @param
     * @return
     */
    @AutoLog(value = "初盘结果管理-创建")
    @ApiOperation(value = "初盘结果管理-创建", notes = "初盘结果管理-创建")
    @PostMapping(value = "/startAssets")
    public Result<?> startAssets(@RequestBody StartAssetsDTO startAssets) throws ParseException {
        //modify and by [qmk] 创建初盘管理单，判断是否所有公司资产已同步
        List<DictModel> dictList =  iSysBaseAPI.queryEnableDictItemsByCode("company_code");
        //获取资产明细中所有的资产
        //todo
        List<String> companyCodeList = assetsItemService.getAllCompanyCode();
        //将资产明细中的公司和字典对比
        for (DictModel dictModel : dictList) {
           if(!companyCodeList.contains(dictModel.getValue())) {
               return Result.error("公司代码"+dictModel.getValue()+"资产未同步，请sap同步后再创建！");
           }
        }
        assetsPreliminaryResultsService.startAssets(startAssets);
        return Result.OK("添加成功！");
    }


    /**
     * 编辑
     *
     * @param assetsPreliminaryResults
     * @return
     */
    @AutoLog(value = "初盘结果管理-编辑")
    @ApiOperation(value = "初盘结果管理-编辑", notes = "初盘结果管理-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<?> edit(@RequestBody AssetsPreliminaryResults assetsPreliminaryResults) {
        assetsPreliminaryResultsService.updateById(assetsPreliminaryResults);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "初盘结果管理-通过id删除")
    @ApiOperation(value = "初盘结果管理-通过id删除", notes = "初盘结果管理-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        assetsPreliminaryResultsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "初盘结果管理-批量删除")
    @ApiOperation(value = "初盘结果管理-批量删除", notes = "初盘结果管理-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.assetsPreliminaryResultsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "初盘结果管理-通过id查询")
    @ApiOperation(value = "初盘结果管理-通过id查询", notes = "初盘结果管理-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        AssetsPreliminaryResults assetsPreliminaryResults = assetsPreliminaryResultsService.getById(id);
        return Result.OK(assetsPreliminaryResults);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param assetsPreliminaryResults
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AssetsPreliminaryResults assetsPreliminaryResults) {
        return super.exportXls(request, assetsPreliminaryResults, AssetsPreliminaryResults.class, "初盘结果管理");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AssetsPreliminaryResults.class);
    }

}
